<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-box">
      <my-search @mySearchClick="gotoSearch"></my-search>
    </view>
    <!-- 搜索组件区域 -->
    <!-- 轮播图区域 -->
    <swiper :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <navigator :url="`/subpkg/goods_dateil/goods_dateil?goods_id=${item.goods_id}`" class="swiper-item">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 轮播图区域结束 -->
    <!-- 分类导航入口区域 -->
    <view class="nav">
      <view class="item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 分类导航入口区域结束 -->
    <!-- 楼层入口区域 -->
    <view class="floor_list">
      <view class="floor_item" v-for="(item,i) in floorList" :key="i">
        <image class="floor-title" :src="item.floor_title.image_src" mode=""></image>
        <view class="floor_imgBox">
          <!-- 左边大图 -->
          <navigator class="left-img-Box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右边四个小图 -->
          <view class="right-img-Box">
            <view class="right-img-item" v-for="(item2,i2) in item.product_list" :key="i2">
              <navigator v-if="i2!==0" :url="item2.url">
                <image :src="item2.image_src" mode="widthFix" :style="{width:item2.image_width + 'rpx'}"></image>
              </navigator>

            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 楼层入口区域结束 -->
  </view>
</template>

<script>
  import badgeMix from '../../mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        //轮播图数据列表
        swiperList: [],
        //分类导航入口数据
        navList: [],
        //楼层数据
        floorList: [],
      };
    },
    created() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      //网络请求轮播图数据的方法
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        console.log(res)
        // 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()
        //请求成功
        this.swiperList = res.message
        uni.$showMsg('加载成功!')
      },
      // 获取分类入口数据
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        console.log(res)
        // 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()
        //请求成功
        this.navList = res.message
        uni.$showMsg('加载成功!')
      },
      // 点击时跳转的事件处理函数
      navClickHandler(item) {
        if (item.name == '分类') {
          return uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },

      //获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // 对请求回来的数据做处理
        res.message.forEach(floor => {
          floor.product_list.forEach(item => {
            item.url = '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      //跳转到搜素页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }

    },

  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    .swiper-item image {
      width: 100%;
      height: 100%;
    }
  }

  .nav {
    height: 180rpx;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .item {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 25%;
      height: 100%;

      image {
        width: 80%;
        height: 80%;
      }
    }
  }

  .floor-title {
    width: 100%;
    height: 60rpx;
  }

  .floor_imgBox {
    display: flex;
    padding-left: 10rpx;
  }

  .right-img-Box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
